package com.example.backend.mapper;

import com.example.backend.entity.DingDan;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.StringJoiner;

public class DingDanMapperProvider {
    public String getUpdateDingDanSql(DingDan dingDan) {
        StringBuffer sql = new StringBuffer("update DingDan set dingdan_num = #{dingdan_num} ");
        if (dingDan.getDingdan_num() != null && dingDan.getDingdan_num().length() > 0) {
            sql.append(" ,dingdan_num = #{dingdan_num} ");
        }
        if (dingDan.getUser_id() != null && dingDan.getUser_id()!=0) {
            sql.append(" ,user_id = #{user_id} ");
        }
        if (dingDan.getShop_id() != null && dingDan.getShop_id()!=0) {
            sql.append(" ,shop_id = #{shop_id} ");
        }
        if (dingDan.getRider_id() != null && dingDan.getRider_id()!=0) {
            sql.append(" ,rider_id = #{rider_id} ");
        }
        if (dingDan.getAddress_id() != null && dingDan.getAddress_id()!=0) {
            sql.append(" ,address_id = #{address_id} ");
        }

        if (dingDan.getState() != null && dingDan.getState().length() > 0) {
            sql.append(" ,state = #{state} ");
        }
        if (dingDan.getMoney() != null && dingDan.getMoney()!= 0) {
            sql.append(" ,money = #{money} ");
        }
        if (dingDan.getRemark() != null && dingDan.getRemark().length() > 0) {
            sql.append(" ,remark = #{remark} ");
        }
        if (dingDan.getTime() != null && dingDan.getTime().length() > 0) {
            sql.append(" ,time = #{time} ");
        }
        if (dingDan.getEnd_time() != null && dingDan.getEnd_time().length() > 0) {
            sql.append(" ,end_time = #{end_time} ");
        }
        if (dingDan.getGratuity() != null && dingDan.getGratuity()!= 0) {
            sql.append(" ,gratuity = #{gratuity} ");
        }
        sql.append(" where dingdan_num = #{dingdan_num}");
        return sql.toString();
    }

    public String getSelectDingDanSql(DingDan dingDan){
        StringBuffer sql = new StringBuffer("select id,dingdan_num,user_id,shop_id,rider_id,address_id,state,money,remark,time,end_time,gratuity from DingDan where 1=1");
        if (dingDan.getId() != null && dingDan.getId()!=0) {
            sql.append(" and id = #{id} ");
        }
        if (dingDan.getDingdan_num() != null && dingDan.getDingdan_num().length() > 0) {
            sql.append(" and dingdan_num = #{dingdan_num} ");
        }
        if (dingDan.getUser_id() != null && dingDan.getUser_id()!=0) {
            sql.append(" and user_id = #{user_id} ");
        }
        if (dingDan.getShop_id() != null && dingDan.getShop_id()!=0) {
            sql.append(" and shop_id = #{shop_id} ");
        }
        if (dingDan.getRider_id() != null && dingDan.getRider_id()!=0) {
            sql.append(" and rider_id = #{rider_id} ");
        }
        if (dingDan.getAddress_id() != null && dingDan.getAddress_id()!=0) {
            sql.append(" and address_id = #{address_id} ");
        }
        if (dingDan.getState() != null && dingDan.getState().length() > 0) {
            if (dingDan.getState().equals("6")) {
                sql.append(" and state in ('0','2','3','4') ");
            }
            else if (dingDan.getState().equals("7")) {
                sql.append(" and state in ('1','5') ");
            }else {
            sql.append(" and state = #{state} ");
             }
        }
        if (dingDan.getMoney() != null && dingDan.getMoney()!= 0) {
            sql.append(" and money = #{money} ");
        }
        if (dingDan.getRemark() != null && dingDan.getRemark().length() > 0) {
            sql.append(" and remark = #{remark} ");
        }
        if (dingDan.getTime() != null && dingDan.getTime().length() > 0) {
            sql.append(" and time = #{time} ");
        }
        if (dingDan.getEnd_time() != null && dingDan.getEnd_time().length() > 0) {
            sql.append(" and end_time = #{end_time} ");
        }
        if (dingDan.getGratuity() != null && dingDan.getGratuity()!= 0) {
            sql.append(" and gratuity = #{gratuity} ");
        }
        return sql.toString();
    }

}
